package org.endy.leisure.diaTracker.controller

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import kotlinx.coroutines.reactor.mono
import org.endy.leisure.diaTracker.entity.exam.ExamAnswer
import org.endy.leisure.diaTracker.entity.exam.ExamListEntity
import org.endy.leisure.diaTracker.service.ExamService
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono

@Tag(name = "在线测试接口", description = "用于在线测试")
@RestController
@RequestMapping("/exam")
class ExamController(private val examService: ExamService) {

    /**
     * 获取试卷列表
     */
    @Operation(summary = "获取试卷列表")
    @PostMapping("/list")
    fun getExamList(@RequestBody request: UserIdRequest): Mono<WebResponse<List<ExamListEntity>>> = mono {
        val list = examService.getExamList(request.userId)
        WebResponse.success(list)
    }

    /**
     * 提交试卷作答
     */
    @Operation(summary = "提交试卷作答")
    @PostMapping("/answer")
    fun submitExamAnswer(@RequestBody request: ExamAnswerRequest): Mono<WebResponse<String>> = mono {
        examService.submitExamAnswer(ExamAnswer(null, request.examId, request.userId, request.answer))
        WebResponse.success("试卷作答成功！")
    }
}